package com.uphone.kingmall.view.psdset;

import android.inputmethodservice.Keyboard;
import android.text.TextUtils;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.china.zhuoyue.R;
import com.lzy.okgo.model.HttpParams;
import com.uphone.kingmall.base.BaseActivity;
import com.uphone.kingmall.utils.ActivityCollector;
import com.uphone.kingmall.utils.CommonUtil;
import com.uphone.kingmall.utils.HttpParamsUtils;
import com.uphone.kingmall.utils.MyUrl;
import com.uphone.kingmall.utils.intent.IntentData;

import butterknife.BindView;

public class WalletPayPassActivity extends BaseActivity {
    public final static String SetPsd = "SetPsd";
    public final static String Pay = "Pay";
    public final static String ReSet = "ReSet";
    public final static String ReSetPsd = "ReSetPsd";
    @BindView(R.id.showPrice)
    TextView showPrice;
    @BindView(R.id.fl)
    FrameLayout fl;
    private String oldPassWord = null;
    private String passWord = null;
    private TextView tv_result;
    @IntentData
    private String para = "";
    @IntentData
    private String code = "";
    @IntentData
    private String clazz = "";
    @IntentData
    private String phone = "";
    private GridPasswordView gpvPlateNumber;


    @Override
    public int getLayoutContentId() {
        return R.layout.kingmall_activity_set_pay_pass;
    }

    @Override
    public void initView() {
        initViews();

    }

    @Override
    public void initData() {

    }

    /**
     * 已经经过密码验证
     * 在这里处理业务逻辑
     **/
    private void doBusiness() {
        if (para == null) {
            return;
        } else if (TextUtils.equals(para, SetPsd)) {
            LogUtils.e("原密码: " + passWord);
            HttpParams params = HttpParamsUtils.getInstance().put("payPass", passWord).getParams();
            CommonUtil.submitNetAndFinish(this, MyUrl.setPayPass, params, "设置成功!");
        } else if (TextUtils.equals(para, Pay)) {
            // 正在支付
            LogUtils.e("原密码: " + passWord);
            if (!TextUtils.isEmpty(clazz)) {
                try {
                    Class<?> aClass = Class.forName(clazz);
                    BaseActivity activity = ActivityCollector.getActivity(aClass);
                    activity.onPayPsdSuccess(passWord);
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
            }
            finish();
        } else if (TextUtils.equals(para, ReSet)) {
            LogUtils.e("原密码: " + passWord);
            HttpParams params = new HttpParams();
            params.put("telephone", phone);
            params.put("payPass", passWord);
            params.put("code", code);
            CommonUtil.submitNetAndFinish(this, MyUrl.setPayPassword, params, "设置密码成功");
        } else if (TextUtils.equals(para, ReSetPsd)) {
            LogUtils.e("原密码: " + passWord);
            HttpParams params = HttpParamsUtils.getInstance()
                    .put("priPass", oldPassWord)
                    .put("newPass", passWord)
                    .getParams();
            CommonUtil.submitNetAndFinish(this, MyUrl.changeUserPayPass, params, "修改密码成功");

        }
    }

    public void initViews() {
        LogUtils.e("para:" + para);
        tv_result = (TextView) findViewById(R.id.showResult);
        String title = "";
        String info = "";
        if (para == null) {
            return;
        } else if (TextUtils.equals(para, Pay)) {
            title = "输入支付密码";
        } else if (TextUtils.equals(para, SetPsd)) {
            title = "设置支付密码";
            info = "请输入新密码";
        } else if (TextUtils.equals(para, ReSet)) {
            info = "请输入新密码";
            title = "找回密码";
            code = getIntent().getStringExtra("code");
            phone = getIntent().getStringExtra("phone");
        } else if (TextUtils.equals(para, ReSetPsd)) {
            info = "请输入旧密码";
            title = "重置支付密码";
        }
        showPrice.setText(info);
        CommonUtil.initTitle(this, title, fl);

        gpvPlateNumber = (GridPasswordView) findViewById(R.id.gpvPlateNumber);
        gpvPlateNumber.setPasswordVisibility(true);
        final XKeyboardView viewKeyboard = (XKeyboardView) findViewById(R.id.view_keyboard);
        viewKeyboard.setKeyboard(new Keyboard(WalletPayPassActivity.this, R.xml.keyboard_number));
        viewKeyboard.setVisibility(View.VISIBLE);
        viewKeyboard.setIOnKeyboardListener(new XKeyboardView.IOnKeyboardListener() {
            @Override
            public void onFinishKeyEvent() {

            }

            @Override
            public void onInsertKeyEvent(String text) {
                gpvPlateNumber.appendPassword(text);
                int length = gpvPlateNumber.getPassWord().trim().length();
                if (length == 6) {
                    viewKeyboard.setEnabled(false);
                    if (TextUtils.equals(para, SetPsd)) {
                        //第一次设置密码
                        if (passWord == null) {
                            passWord = gpvPlateNumber.getPassWord();
                            ToastUtils.showLong("请确认密码");
                            showPrice.setText("请确认密码");
                            gpvPlateNumber.clearPassword();
                        } else {
//                            第二次输入密码
                            String repass = gpvPlateNumber.getPassWord().trim();
                            if (TextUtils.equals(passWord, repass)) {
                                LogUtils.e("设置密码中……");
                                ToastUtils.showLong("正在设置密码……");
                                doBusiness();
                            } else {
                                ToastUtils.showLong("密码错误，请重新输入");
                                showPrice.setText("密码错误，请重新输入");
                                gpvPlateNumber.clearPassword();
                            }
                        }
                    } else if (TextUtils.equals(para, Pay)) {
                        //支付
                        passWord = gpvPlateNumber.getPassWord();
                        doBusiness();
                    } else if (TextUtils.equals(para, ReSet)) {
                        //重置支付密码
                        if (passWord == null) {
                            passWord = gpvPlateNumber.getPassWord();
                            ToastUtils.showLong("请确认密码");
                            showPrice.setText("请确认密码");
                            gpvPlateNumber.clearPassword();
                        } else {
                            String repass = gpvPlateNumber.getPassWord().trim();
                            if (TextUtils.equals(passWord, repass)) {
                                ToastUtils.showLong("密码正确");
                                showPrice.setText("");
                                doBusiness();
                            } else {
                                ToastUtils.showLong("两次密码不一致，请重新输入");
                                showPrice.setText("请确认密码");
                                gpvPlateNumber.clearPassword();
                            }
                        }

                    } else if (TextUtils.equals(para, ReSetPsd)) {
                        //重置支付密码
                        if (oldPassWord == null) {
                            oldPassWord = gpvPlateNumber.getPassWord();
                            ToastUtils.showLong("请输入新密码");
                            showPrice.setText("请输入新密码");
                            gpvPlateNumber.clearPassword();
                        } else if (passWord == null) {
                            passWord = gpvPlateNumber.getPassWord();
                            ToastUtils.showLong("请确认密码");
                            showPrice.setText("请确认密码");
                            gpvPlateNumber.clearPassword();
                        } else {
                            String repass = gpvPlateNumber.getPassWord().trim();
                            if (TextUtils.equals(passWord, repass)) {
                                ToastUtils.showLong("密码正确");
                                showPrice.setText("");
                                doBusiness();
                            } else {
                                ToastUtils.showLong("两次密码不一致，请重新输入");
                                showPrice.setText("请确认密码");
                                gpvPlateNumber.clearPassword();
                            }
                        }

                    }

                } else {
                    viewKeyboard.setEnabled(true);
                }
            }

            @Override
            public void onDeleteKeyEvent() {
                gpvPlateNumber.deletePassword();
            }
        });

        gpvPlateNumber.togglePasswordVisibility();
        gpvPlateNumber.setOnPasswordChangedListener(new GridPasswordView.OnPasswordChangedListener() {
            @Override
            public boolean beforeInput(int position) {
                viewKeyboard.setVisibility(View.VISIBLE);
                return true;
            }

            @Override
            public void onTextChanged(String psw) {

            }

            @Override
            public void onInputFinish(String psw) {

            }
        });
    }


    //                    // 支付
//                    if (没有密码){
//                        设置密码
//                    }else {
//                    if (密码不正确){
//                        
//                    }else {
//                    if(有余额){
//                        成功
//                    }else{
//                        余额不足
//                        if(充值){
//                            充值
//                        } else{
//                            关闭
//                        }
//                    }
//                    }
//                    }
}
